<template>
  <div class="app-container">
    <div class="item2">
      <div class="header">
        <div class="title1">
          <i class="el-icon-data-analysis"></i><span>设备状态控制</span>
        </div>
        <div class="backB">
          <img src="@/assets/images/back.png" alt="" @click="goBack" />
        </div>
      </div>
      <div class="devicedataTable">
        <div class="leftTable">
          <div class="deviceTitle">设备启动状态修改</div>
          <div class="deviceContent">
            <div
              class="deviceContentItem"
              v-for="(item, index) in tableData"
              :key="index"
            >
              <div class="title">
                {{ item.deviceId | getDictValue(that) }}({{ item.deviceType }})
              </div>
              <div class="startStatus">
                <div>启动灯状态:</div>
                <div>
                  <el-image
                    class="light"
                    :src="item.startLight == 1 ? greenurl : redurl"
                  ></el-image>
                </div>
                <div>停止灯状态:</div>
                <div>
                  <el-image
                    class="light"
                    :src="item.stopLight == 1 ? greenurl : redurl"
                  ></el-image>
                </div>
              </div>
              <div class="threeBtn">
                <el-button
                  type="success"
                  round
                  @click="handleStart(item.deviceId, 1)"
                  >启动</el-button
                >
                <el-button
                  type="danger"
                  round
                  @click="handleStop(item.deviceId, 2)"
                  >停止</el-button
                >
                <el-button
                  type="warning"
                  round
                  @click="handleReset(item.deviceId, 3)"
                  >复位</el-button
                >
              </div>
            </div>
          </div>
        </div>
        <div class="rightbindBox">
          <!-- <div class="rightbindTitle">叠箱机设置</div> -->
          <div class="backflow">
            <div class="rightbindTitle">绑箱/回流设置</div>
            <div class="eisContent">
              <div
                class="eisItem"
                v-for="(item, index) in bindlist"
                :key="index"
              >
                <div class="eisTitle">{{ item.paramDesc }}</div>
                <div class="eisSwitch">
                  <el-switch
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    v-model="item.paramValue"
                    active-value="1"
                    inactive-value="0"
                    @change="handleEditparams(item)"
                  ></el-switch>
                </div>
              </div>
            </div>
          </div>
          <div class="casestacker">
            <div class="rightbindTitle">叠箱机设置</div>
            <div class="caseItem">
              <div class="caseBox">
                <div class="caseTitle">叠箱机({{ stackerDeviceType }})</div>
                <div class="caseNum">
                  <div class="caseNumTitle">最大堆叠数量:</div>
                  <el-input
                    v-model="stackerValue"
                    placeholder="请输入最大堆叠数量"
                  ></el-input>
                </div>
                <div class="caseBtn">
                  <el-button type="success" round @click="handleMax"
                    >设置最大堆叠数量</el-button
                  >
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="footer">
      <el-dialog
        title="操作验证"
        :visible.sync="centerDialogVisible"
        width="22%"
        left
        :close-on-click-modal="false"
      >
        <el-form
          :model="passForm"
          status-icon
          :rules="passrules"
          ref="passForm"
          label-width="60px"
          class="demo-ruleForm"
        >
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model="passForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit('passForm')"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  devicelist,
  deviceEdit,
  caseStackerlist,
  caseStackerEdit,
} from "@/api/device";
import { eisconfigList, eisconfigedit } from "@/api/eisconfig";
import { getcheckPassword } from "@/api/subSystem/continer";
export default {
  dicts: ["sys_pallet_line"],
  data() {
    var passwordError = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("密码不能为空"));
      } else {
        getcheckPassword(value).then((res) => {
          this.priorityNum = res.msg;
          if (res.msg == "admin") {
            callback();
          } else if (res.msg == "supper") {
            callback();
          } else if (res.msg == "false") {
            callback(new Error("密码错误"));
          }
        });
      }
    };
    return {
      greenurl: require("@/assets/images/greenlight.gif"),
      redurl: require("@/assets/images/redlight.gif"),
      // 密码验证
      centerDialogVisible: false,
      passForm: {
        pass: null,
      },
      passrules: {
        pass: [{ validator: passwordError, trigger: "blur" }],
      },
      // 总条数
      total: 0,
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        // isLock: null,
        // stationNo:null,
      },
      formRules: {
        // stationNo: [
        //   { required: false, message: "请输入站点编号", trigger: "blur" },
        // ],
      },
      tableData: [],
      loading: false,
      //   serial  序列号  sid  会话ID
      // deviceIdlist: [
      //   { value: "27", label: "一楼托盘线" },
      //   { value: "26", label: "四楼托盘线" },
      // ],
      devicestatuslist: [
        { value: "1", label: "启动" },
        { value: "2", label: "停止" },
        { value: "3", label: "复位" },
      ],
      deviceId: "",
      status: "",
      // 点击公共值
      btntouchValue: 0,
      // eis数据值
      bindlist: [],
      paramsName: "",
      paramsId: "",
      // 叠箱机数据
      stackerDeviceid: "",
      stackerDeviceType: "",
      stackerValue: "",
      that: this,
    };
  },
  filters: {
    getDictValue(value, that) {
      // console.log(value, that);
      return that.selectDictLabel(that.dict.type.sys_pallet_line, value);
    },
  },
  mounted() {
    this.handleQuery();
    this.eisconfigdataList();
    this.casestackerList();
  },
  methods: {
    // 叠箱机列表
    casestackerList() {
      caseStackerlist().then((res) => {
        const { deviceId, deviceType, stackValue } = res.data[0];
        this.stackerDeviceid = deviceId;
        this.stackerDeviceType = deviceType;
        this.stackerValue = stackValue;
      });
    },
    // 设置叠箱最大数
    handleMax() {
      console.log(this.stackerDeviceid, this.stackerValue);
      this.centerDialogVisible = true;
      this.passForm.pass = null;
      this.btntouchValue = 3;
    },
    eisconfigdataList() {
      eisconfigList().then((res) => {
        this.bindlist = res.data;
      });
    },
    // switch按钮绑箱修改
    handleEditparams(row) {
      this.paramsName = row.paramNo;
      this.paramsId = row.paramValue;
      console.log(this.paramsName, this.paramsId);
      // this.centerDialogVisible = true;
      // this.passForm.pass = null;
      // this.btntouchValue = 2;
      if (row.paramNo == "B2F101" || row.paramNo == "B2F301") {
        this.$message({
          message: "功能已禁用",
          type: "warning",
        });
        this.eisconfigdataList();
      } else {
        this.centerDialogVisible = true;
        this.passForm.pass = null;
        this.btntouchValue = 2;
      }
    },
    goBack() {
      this.$router.push("/subSystem1");
    },
    handleQuery() {
      devicelist().then((res) => {
        this.tableData = res.data;
        // this.total = res.total;
        this.loading = false;
      });
    },
    // handleStatus() {
    //   this.centerDialogVisible = true;
    //   this.passForm.pass = null;
    //   this.btntouchValue = 1;
    //   console.log(this.deviceId, this.status);
    // },
    // 共有封装
    handleCommon() {
      this.centerDialogVisible = true;
      this.passForm.pass = null;
      this.btntouchValue = 1;
      console.log(this.deviceId, this.status);
    },
    // 设备启动
    handleStart(deviceid, num) {
      this.deviceId = deviceid;
      this.status = num;
      this.handleCommon();
    },
    // 设备停止
    handleStop(deviceid, num) {
      this.deviceId = deviceid;
      this.status = num;
      this.handleCommon();
    },
    // 设备复位
    handleReset(deviceid, num) {
      this.deviceId = deviceid;
      this.status = num;
      this.handleCommon();
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.btntouchValue == 1) {
            console.log(this.deviceId, this.status);
            deviceEdit({
              deviceId: this.deviceId,
              status: parseInt(this.status),
            }).then((res) => {
              console.log(res);
              this.handleQuery();
            });
          } else if (this.btntouchValue == 2) {
            console.log(this.paramsName, this.paramsId);
            eisconfigedit({
              paramNo: this.paramsName,
              paramValue: this.paramsId,
            }).then((res) => {
              this.eisconfigdataList();
            });
          } else if (this.btntouchValue == 3) {
            caseStackerEdit({
              deviceId: this.stackerDeviceid,
              stackValue: parseInt(this.stackerValue),
            }).then((res) => {
              this.casestackerList();
            });
          }
          this.centerDialogVisible = false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  background: #2142fd;
  .el-icon-data-analysis::before {
    color: #00d9ff;
    margin-right: 8px;
  }
  .title1 {
    font-size: 16px;
    // margin-bottom: 15px;
    // color: #808080;
    color: #fff;
    width: 150px;
    padding: 5px;
    border-bottom: 1px dotted #8ddfff;
  }
  .header {
    margin-top: -10px;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 220px;
      margin-right: 20px;
    }

    .backB {
      cursor: pointer;

      img {
        width: 20%;
      }
    }
  }
  .formCenter {
    height: 32px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 10px;
  }
  .footer {
    margin-top: 20px;
    width: 100%;
    min-height: 200px;
  }
  .devicedataTable {
    width: 100%;
    // display: flex;
    // justify-content: space-between;
    .leftTable {
      // width: 49%;
      .deviceTitle {
        font-size: 14px;
        color: #fff;
        margin: 10px 0;
        width: 130px;
        padding-bottom: 5px;
        border-bottom: 1px dotted #8ddfff;
      }
      .deviceContent {
        width: 100%;
        display: flex;
        justify-content: flex-start;
        text-align: center;
        flex-wrap: wrap;
        .deviceContentItem {
          background: #0199ff;
          color: #fff;
          margin: 15px;
          text-align: center;
          width: 280px;
          padding: 10px;
          .title {
            font-size: 18px;
            font-weight: 700;
          }
          .startStatus {
            display: flex;
            justify-content: space-between;
            padding: 10px;
            align-items: center;
            .light {
              ::v-deep .el-image__inner {
                width: 20px;
                height: 20px;
                margin-top: 40%;
                border-radius: 50%;
                border: 2px solid #c5c5c5;
                // box-shadow: 0 0 10px rgba(117, 117, 117, 0.445);
              }
            }
          }
          .threeBtn {
            display: flex;
            justify-content: space-between;
            align-items: center;
          }
        }
      }

      .deviceStatus {
        margin-top: 30px;
        .deviceTable {
          width: 100%;
          padding: 10px 50px;
          display: flex;
          justify-content: center;
          align-items: center;
          .deviceItem {
            margin-right: 30px;
          }
        }
      }
    }
    .rightbindBox {
      // width: 49%;
      display: flex;
      justify-content: flex-start;
      .backflow {
        width: 50%;
        .rightbindTitle {
          color: #fff;
          margin: 20px 0 20px 0;
          width: 110px;
          padding-bottom: 5px;
          border-bottom: 1px dotted #8ddfff;
        }
        .eisContent {
          width: 100%;
          height: 400px;
          display: flex;
          justify-content: flex-start;
          align-items: center;
          flex-wrap: wrap;
          .el-switch {
            transform: scale(1.5);
          }

          .eisItem {
            background: #18dcff;
            width: 280px;
            height: 150px;
            line-height: 4;
            text-align: center;
            margin: 10px;
            .eisTitle {
              text-align: center;
              color: #fff;
              font-size: 18px;
              font-weight: 700;
            }
          }
        }
      }
      .casestacker {
        width: 50%;
        .rightbindTitle {
          color: #fff;
          margin: 20px 0 20px 0;
          width: 90px;
          padding-bottom: 5px;
          border-bottom: 1px dotted #8ddfff;
        }
        .caseItem {
          display: flex;
          justify-content: center;
          height: 400px;
          align-items: center;
          .caseBox {
            width: 300px;
            height: 200px;
            background: #7d5fff;
            padding: 20px;
            .caseTitle {
              text-align: center;
              color: #fff;
              font-size: 18px;
              font-weight: 700;
            }
            .caseNum {
              padding: 10px;
              // display: flex;
              // align-items: center;
              .caseNumTitle {
                text-align: center;
                color: #fff;
                margin-bottom: 10px;
              }
            }
            .caseBtn {
              text-align: center;
            }
          }
        }
      }
    }
  }
}
</style>